<!DOCTYPE html>
<html>

<head>
    <title>登录</title>
    <style>
        body {
            background-color: #f4f4f9;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        .login-box {
            width: 350px;
            margin: 100px auto;
            padding: 30px;
            border: none;
            border-radius: 10px;
            background-color: #ffffff;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
        }

        h2 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        input {
            margin: 15px 0;
            width: 100%;
            padding: 12px;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #0056b3;
        }

        a {
            display: block;
            text-align: center;
            margin-top: 20px;
            color: #007BFF;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
    <div class="login-box">
        <h2>用户登录</h2>
        <input type="text" id="username" placeholder="用户名">
        <input type="password" id="password" placeholder="密码">
        <button onclick="submitLogin()">登录</button>
        <a href="/register">注册新用户</a>
    </div>

    <script>
        function submitLogin() {
            const data = {
                username: document.getElementById("username").value,
                password: document.getElementById("password").value
            };

            fetch("/login", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify(data)
            })
                .then(response => {
                    if (response.ok) {
                        setTimeout(() => {
                            window.location.href = "/";
                        }, 100);
                    } else {
                        alert("登录失败");
                    }
                })
                .catch(error => {
                    console.error('登录错误:', error);
                    alert("登录失败，请重试");
                });
        }
    </script>
</body>

</html>